package com.mzy.basics.utils.Java;

/**
 * @author Ives
 * * @date 2022/2/9
 */
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * <p>
 * Title: StringUtil
 * </p>
 * <p>
 * Description:
 * </p>
 *
 * @author sunys
 * @date 2018年7月10日
 */

public final class StringUtil {

  private static Logger logger = LoggerFactory.getLogger(StringUtil.class);

  public static final String EMPTY = "";

  private StringUtil() {

  }

  /**
   * 下划线转驼峰
   *
   * @param value
   * @return
   */
  public static String underline2Camel(String value) {
    if (StringUtils.isEmpty(value)) {
      return null;
    }
    String temp = value.toLowerCase();
    final StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile("_(\\w)");// 表示一个“字”（数字，字符，下划线）
    Matcher m = p.matcher(temp);
    while (m.find()) {
      // appendReplacement：将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里
      m.appendReplacement(sb, m.group(1).toUpperCase());
    }
    // appendTail：将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里
    m.appendTail(sb);

    String rtnValue = sb.toString();
    logger.info("下划线转驼峰：" + value + " --> " + rtnValue);
    return rtnValue;
  }

  /**
   *
   * <p>
   * Title: 方法说明
   * </p>
   * <p>
   * Description: 左侧补零
   * </p>
   *
   * @param value
   * @return
   */
  public static String leftPatchZero(int length, long value) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
      sb.append("0");
    }
    DecimalFormat df = new DecimalFormat(sb.toString());
    String str = df.format(value);
    return str;
  }

  /**
   * <p>
   * Title: 替换请求中的下划线、百分号特殊字符，防止sql查询注入
   * </p>
   * <p>
   * Description:
   * </p>
   *
   * @param content
   * @return
   */
  public static String replaceUnderlineOrPercent(String content) {
    final String SOURCE_UNDERLINE = "_";
    final String SOURCE_PERCENT = "%";
    final String SOURCE_SLASHLINE = "\\\\";
    final String TARGET_UNDERLINE = "\\\\_";
    final String TARGET_PERCENT = "\\\\%";
    final String TARGET__SLASHLINE = "\\\\\\\\";
    if (StringUtils.hasText(content)) {
      String result = content.replaceAll(SOURCE_SLASHLINE, TARGET__SLASHLINE)
              .replaceAll(SOURCE_UNDERLINE, TARGET_UNDERLINE)
              .replaceAll(SOURCE_PERCENT, TARGET_PERCENT);
      return result;
    } else {
      return content;
    }
  }

  /**
   * 如String对象为null，则返回为空字符串；否则去掉前后空格后返回。
   *
   * @param content 需处理的字符串
   * @return 处理后的字符串
   * @author Tom 2018/12/26
   */
  public static String nullToStringTrim(String content) {
    if (null == content) {
      return "";
    } else {
      return content.trim();
    }
  }

  public static boolean isEmpty(final CharSequence cs) {
    return cs == null || cs.length() == 0;
  }

  public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
      return true;
    }
    for (int i = 0; i < strLen; i++) {
      if (!Character.isWhitespace(cs.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static String join(final Object[] array, final char separator, final int startIndex, final int endIndex) {
    if (array == null) {
      return null;
    }
    final int noOfItems = endIndex - startIndex;
    if (noOfItems <= 0) {
      return EMPTY;
    }
    final StringBuilder buf = new StringBuilder(noOfItems * 16);
    for (int i = startIndex; i < endIndex; i++) {
      if (i > startIndex) {
        buf.append(separator);
      }
      if (array[i] != null) {
        buf.append(array[i]);
      }
    }
    return buf.toString();
  }

  public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
    if (array == null) {
      return null;
    }
    if (separator == null) {
      separator = EMPTY;
    }
    final int noOfItems = endIndex - startIndex;
    if (noOfItems <= 0) {
      return EMPTY;
    }
    final StringBuilder buf = new StringBuilder(noOfItems * 16);
    for (int i = startIndex; i < endIndex; i++) {
      if (i > startIndex) {
        buf.append(separator);
      }
      if (array[i] != null) {
        buf.append(array[i]);
      }
    }
    return buf.toString();
  }

  public static String join(final Object[] array, final String separator) {
    if (array == null) {
      return null;
    }
    return join(array, separator, 0, array.length);
  }

  public static boolean isNotBlank(final CharSequence cs) {
    return !isBlank(cs);
  }

  public static int appearNumber(String srcText, String findText) {
    int count = 0;
    Pattern p = Pattern.compile(findText);
    Matcher m = p.matcher(srcText);
    while (m.find()) {
      count++;
    }
    return count;
  }
}